﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DnPakExplorer.Controls {
	public partial class ImageButton : UserControl {

		public enum ButtonState {
			Normal, Hovered, Pressed,
		}

		ButtonState mButtonState = ButtonState.Normal;
		Image mNormalBackImage;
		Image mHoveredBackImage;
		Image mPressedBackImage;

		[Browsable(false)]
		public ButtonState State{
			get { return mButtonState; }
			private set {
				if (mButtonState != value) {
					mButtonState = value;
					switch (mButtonState) {
					case ButtonState.Normal:
						BackgroundImage = NormalBackImage;
						break;
					case ButtonState.Hovered:
					case ButtonState.Pressed:
						BackgroundImage = HoveredBackImage;
						break;
					}
				}
			}
		}

		public Image NormalBackImage {
			get { return mNormalBackImage; }
			set {
				mNormalBackImage = value;
				if (State == ButtonState.Normal) {
					BackgroundImage = value;
				}
			}
		}

		public Image HoveredBackImage {
			get { return mHoveredBackImage; }
			set {
				mHoveredBackImage = value;
				if (State == ButtonState.Hovered) {
					BackgroundImage = value;
				}
			}
		}

		public Image PressedBackImage {
			get { return mPressedBackImage; }
			set {
				mPressedBackImage = value;
				if (State == ButtonState.Pressed) {
					BackgroundImage =value;
				}
			}
		}

		public ImageButton() {
			InitializeComponent();
			SetStyle(ControlStyles.OptimizedDoubleBuffer| ControlStyles.ResizeRedraw|ControlStyles.AllPaintingInWmPaint, true);
			BackgroundImageLayout = ImageLayout.Stretch;
		}

		private void ImageButton_Paint(object sender, PaintEventArgs e) {
		}

		private void ImageButton_MouseUp(object sender, MouseEventArgs e) {
			State = ButtonState.Normal;
		}

		private void ImageButton_MouseDown(object sender, MouseEventArgs e) {
			State = ButtonState.Pressed;
		}

		private void ImageButton_MouseLeave(object sender, EventArgs e) {
			State = ButtonState.Normal;
		}

		private void ImageButton_MouseMove(object sender, MouseEventArgs e) {
			if(State != ButtonState.Pressed)
				State = ButtonState.Hovered;
		}
	}
}
